package com.bjyhl.medical.main.recommend;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bjyhl.core.fragments.bottom.BottomItemFragment;
import com.bjyhl.core.net.RestClient;
import com.bjyhl.core.net.callback.IError;
import com.bjyhl.core.net.callback.IFailure;
import com.bjyhl.core.net.callback.ISuccess;
import com.bjyhl.medical.R;
import com.bjyhl.ui.recycler.MultipleRecyclerAdapter;
import com.bjyhl.ui.refresh.RefreshHandler;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.SimpleClickListener;

public class RecommendFragment extends BottomItemFragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;
    private RefreshHandler mRefreshHandler;

    @Override
    public Object setLayout() {
        return R.layout.fragment_recommend;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View root) {
        mRecyclerView = $(R.id.rv_recc);
        mSwipeRefreshLayout = $(R.id.srl_recc);
        mRefreshHandler = RefreshHandler.create(mSwipeRefreshLayout, mRecyclerView, new RecommendDataConverter(),
                "http://192.168.8.101:3000/v1/pad/recommend", false);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initRefreshLayout();
        initRecyclerView();
        mRefreshHandler.firstPage();



        RestClient.builder()
                .file("/storage/sdcard/sdcard/jdad_20191018_152837.jpg")
                .url("http://10.3.100.100:3009/upload")
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LogUtils.d("下载成功:" + response);
                    }
                })
                .failure(new IFailure() {
                    @Override
                    public void onFailure() {
                        Log.e("TAG", "onFailure: ");
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        Log.e("TAG", "onError: " + code + "@" + msg);
                    }
                })
                .build().upload();
    }

    private void initRefreshLayout() {
        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light
        );
        mSwipeRefreshLayout.setProgressViewOffset(true, 120, 300);
    }

    private void initRecyclerView() {
        GridLayoutManager manager = new GridLayoutManager(getContext(), MultipleRecyclerAdapter.SPAN_SIZE_TOTAL, GridLayoutManager.VERTICAL, false);

        mRecyclerView.setLayoutManager(manager);
//        mRecyclerView.addItemDecoration(new SpaceItemDecoration(10));
        mRecyclerView.addOnItemTouchListener(new SimpleClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) { }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) { }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildLongClick(BaseQuickAdapter adapter, View view, int position) { }
        });
    }
}
